package com.application.Controller;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import android.util.Log;

public class ThreadSendFile extends Thread{
	String _filenamesend = "";
	String _path = "";
	String _keychat="";
	String _type = "";
	int _choice = 0;
	int _portnumber = 0;
	boolean _bl = true;
	int _iSoLuongTai = 0;
	boolean _isRunning = true;
	int _typeSend = 0;
	String _title = "";
	int _idDetailLearning = 0;
	ArrayList<Address> _arrDSUser;
	public void setSendFile(String path, String keychat, int port, String type, ArrayList<Address> arrDSGourp)
	{
		_portnumber = port;
		_choice = 1;
		_path = path;
		_filenamesend = new File(path).getName();
		_keychat = keychat;
		_arrDSUser = arrDSGourp;
		_type = type;
	}
	
	public void setSendFile(String path, String keychat, int port, int idDetail, String title )
	{
		_portnumber = port;
		_choice = 1;
		_path = path;
		_filenamesend = new File(path).getName();
		_keychat = keychat;
		_title = title;
		_idDetailLearning = idDetail;
		_typeSend =1;
	}
	public void setboolean(boolean value)
	{
		_bl = value;
	}
	public void Resume()
	{
		_isRunning = true;
	}
	public void Pause()
	{
		_isRunning = false;
	}
	@Override
	public void run()
	{
		Log.d("PEERDROID","vao thread run");	
		try{	
			int i =0;
			while(true)
			{
				if(_choice == 1 && _isRunning)
				{
					Thread runThread = Thread.currentThread();
					if(_typeSend != 0)// send file bình thường
						NetworkController.sendFeedbackGroup(NetworkController.get_ipHost(), _keychat, _filenamesend, _portnumber, _idDetailLearning, _title);
					else
						NetworkController.sendFileChatGroup(_filenamesend, _keychat, _portnumber, _type, _arrDSUser);
					SendFile(_path, _portnumber);
					Thread.sleep(200);
					this.suspend();
				}
			}
		}
		catch(Exception ex)
		{
		}
	}
	
	public void SendFile(String Path, int portnumber)
	{
		 try{
			 Log.d("PEERDROID","vao send file");	
		 	ServerSocket servsock = new ServerSocket(portnumber);
		    while (_bl) {
		    	Log.d("PEERDROID","waiting...");
	
		      Socket sock = servsock.accept();
		      System.out.println("Accepted connection : " + sock);
	
		      // sendfile
		      Log.d("PEERDROID","PATH: " + Path);	
		      File myFile = new File (Path);
		      
		      byte [] mybytearray  = new byte [(int)myFile.length()];
		      FileInputStream fis = new FileInputStream(myFile);
		      BufferedInputStream bis = new BufferedInputStream(fis);
		      bis.read(mybytearray,0,mybytearray.length);
		      OutputStream os = sock.getOutputStream();
		      Log.d("PEERDROID","Sending...");
		      os.write(mybytearray,0,mybytearray.length);
		      os.flush();
		      sock.close();
		      Log.d("PEERDROID","Sending Ok");
		      _iSoLuongTai ++;
		      Log.d("PEERDROID","_so luong tai:" + _iSoLuongTai);
		      Log.d("PEERDROID","_so luong tai:" + _arrDSUser.size());
		      if(_iSoLuongTai >= _arrDSUser.size()-1)
		      {
		    	  _isRunning = false;
		    	  setboolean(false);
		    	  break;
		      }
		    }
		 }
		 catch(Exception ex){}
		 stop();
	}
	
}
